package dao;

// Generated Nov 19, 2011 3:40:07 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;

import pojo.Genre;

/**
 * Home object for domain model class Genre.
 * @see pojo.Genre
 * @author Hibernate Tools
 */
public class GenreHome {

    private static final Log log = LogFactory.getLog(GenreHome.class);

    private final SessionFactory sessionFactory = getSessionFactory();

    protected SessionFactory getSessionFactory() {
	try {
	    return (SessionFactory) HibernateUtil.getSessionFactory();
	} catch (Exception e) {
	    log.error("Could not locate SessionFactory in JNDI", e);
	    throw new IllegalStateException(
		    "Could not locate SessionFactory in JNDI");
	}
    }

    public void persist(Genre transientInstance) {
	log.debug("persisting Genre instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.persist(transientInstance);
	    log.debug("persist successful");
	    session.close();
	} catch (RuntimeException re) {
	    log.error("persist failed", re);
	    throw re;
	}
    }

    public void attachDirty(Genre instance) {
	log.debug("attaching dirty Genre instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.saveOrUpdate(instance);
	    log.debug("attach successful");
	    session.close();
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }

    public void attachClean(Genre instance) {
	log.debug("attaching clean Genre instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	    session.close();
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }

    public void delete(Genre persistentInstance) {
	log.debug("deleting Genre instance");
	try {
	    Session session = sessionFactory.openSession();
	    session.delete(persistentInstance);
	    log.debug("delete successful");
	    session.close();
	} catch (RuntimeException re) {
	    log.error("delete failed", re);
	    throw re;
	}
    }

    public Genre merge(Genre detachedInstance) {
	log.debug("merging Genre instance");
	try {
	    Session session = sessionFactory.openSession();
	    Genre result = (Genre) session.merge(
		    detachedInstance);
	    log.debug("merge successful");
	    session.close();
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public Genre findById(java.lang.Integer id) {
	log.debug("getting Genre instance with id: " + id);
	try {
	    Session session = sessionFactory.openSession();
	    Genre instance = (Genre) session.get(
		    "pojo.Genre", id);
	    if (instance == null) {
		log.debug("get successful, no instance found");
	    } else {
		log.debug("get successful, instance found");
	    }
	    session.close();
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List findByExample(Genre instance) {
	log.debug("finding Genre instance by example");
	try {
	    Session session = sessionFactory.openSession();
	    List results = session
		    .createCriteria("pojo.Genre").add(Example.create(instance))
		    .list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    session.close();
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }
}
